package org.dfl.plugin.dialogs;

import java.util.Set;

import org.dfl.core.model.transitions.PiTransition;
import org.dfl.messages.Messages;
import org.dfl.plugin.editor.model.TransitionModelElement;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Shell;

/**
 * dialog to change pi transition properties
 * @author Piotr Wlodarczyk
 *
 */
public class PiTransitionPropertiesDialog extends TransitionPropertiesDialog {
	private Combo keyCombo;

	public PiTransitionPropertiesDialog(TransitionModelElement t) {
		super(t);
	}

	@Override
	protected void addSpecificTransitionElements(final Shell parent){
		if(getTransition() instanceof PiTransition){
			PiTransition piTransition = (PiTransition) getTransition() ;

			Set<String> available = piTransition.getAvailableSubkeys();
			keyCombo = addComboRow(parent,true, false, Messages.PiTransitionPropertiesDialog_subkey, available, piTransition.getSubkey());
			setFieldErrorChecking(keyCombo, keyCombo.getText()==null, Messages.PiTransitionPropertiesDialog_0);
			keyCombo.addModifyListener(new ModifyListener(){
				public void modifyText(ModifyEvent e) {
					setFieldErrorChecking(keyCombo, keyCombo.getText()==null, Messages.PiTransitionPropertiesDialog_0);
				}});
			addEnterKeyListener(keyCombo);
		}
	}
	
	@Override
	protected void finish() {
		super.finish();
		try {
			getTransition().setSubkey(keyCombo.getText());
		} catch (Exception e) {
			setFieldErrorChecking(keyCombo, true, e.getMessage());
		}
	}
	
	@Override
	public PiTransition getTransition() {
		return (PiTransition) super.getTransition();
	}
}
